package com.winning.bi.etl;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;
import com.winning.bi.jdbc.dao.Dao;

/**
 * 
 * @author 张大川
 * 
 *         数据元数据对比，
 *
 */
public class DataList extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	String[][] strings;
	String[][] strings2;
	JLabel jLabel;
	String[][] tableData;
	DefaultTableModel tableModels;
	String returnString;
	ClassPathXmlApplicationContext ac;
	List<String[]> list = new ArrayList<String[]>();

	public String getReturnString() {
		return returnString;
	}

	public void setReturnString(String returnString) {
		this.returnString = returnString;
	}

	public ClassPathXmlApplicationContext getAc() {
		return ac;
	}

	public void setAc(ClassPathXmlApplicationContext ac) {
		this.ac = ac;
	}

	Map<String, String> map;

	/**
	 * Launch the application.
	 * 
	 * /** Create the frame.
	 */
	public DataList(String source, String target) {

		super("对应关系");

		map = new HashMap<String, String>();

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			}

		});
		setBounds(100, 100, 450, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		JPanel text1 = new JPanel();
		jLabel = new JLabel();

		JScrollPane jPanel = new JScrollPane();

		JScrollPane jScrollPane1 = new JScrollPane();

		JScrollPane jScrollPane = new JScrollPane();

		ac = new ClassPathXmlApplicationContext("file:config/applicationContext.xml");
		Dao dao = (Dao) ac.getBean("daoSource");
		Dao dao2 = (Dao) ac.getBean("daoTarget");
		strings = dao.getMetaDataArray(source);
		strings2 = dao2.getMetaDataArray(target);
		final Object[] columnNames = { "源字段名", "数据类型", "长度" };

		JTable jtable1 = new JTable(strings, columnNames);

		jScrollPane1.setViewportView(jtable1);
		final Object[] columnNames2 = { "目标字段名", "数据类型", "长度" };
		String[] strings1 = { "源字段", "目标字段" };
		tableModels = new DefaultTableModel(tableData, strings1);
		JTable jTable = new JTable(tableModels);
		jTable.setEnabled(false);
		jTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int c = e.getButton();

				if (c == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
					int isDelete = JOptionPane.showConfirmDialog(contentPane, "确定删除这条记录", "提示",
							JOptionPane.YES_NO_OPTION);
					int row = jTable.rowAtPoint(e.getPoint());

					if (row >= 0) {
						jTable.setRowSelectionInterval(row, row);
					}

					if (isDelete == JOptionPane.YES_OPTION) {
						System.out.println("删除");
						int i = jTable.getSelectedRow();

						Object[] objects = tableData[i];
						map.remove(objects[0]);
						list.clear();
						for (Map.Entry<String, String> entry : map.entrySet()) {

							String[] rdata = new String[2];
							rdata[0] = entry.getKey();
							rdata[1] = entry.getValue();
							list.add(rdata);
						}
						tableData = new String[list.size()][];
						for (int k = 0; k < list.size(); k++) {
							tableData[k] = list.get(k);
						}
						System.out.println(JSON.toJSON(tableData));
						tableModels.setDataVector(tableData, strings1);
						tableModels.fireTableDataChanged();
					} else {
						return;
					}

				}

			}

		});

		JTable jtable2 = new JTable(strings2, columnNames2);
		jScrollPane1.setPreferredSize(new Dimension(200, 200));
		jScrollPane.setPreferredSize(new Dimension(200, 200));
		jScrollPane.setViewportView(jtable2);

		jScrollPane1.setViewportView(jtable1);
		JButton button = new JButton("添加对应");
		button.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {

				int i = jtable1.getSelectedRow();
				int j = jtable2.getSelectedRow();

				if (i > -1 && j > -1) {
					String[] objects = strings[i];
					String[] objects2 = strings2[j];

					/**
					 * 
					 * 除特殊情况，不允许不同数据类型之间映射
					 * 
					 */
					if (ArrayUtils.contains(NewTask.KEYS, objects[0].toUpperCase())) {
						objects[0] = "[" + objects[0] + "]";

					} else if (ArrayUtils.contains(NewTask.KEYS, objects2[0].toUpperCase()))

					{
						JOptionPane.showMessageDialog(contentPane,
								"字段包含数据库关键字"+objects2[0], "提示",
								JOptionPane.WARNING_MESSAGE);
						return;
					} else if (!objects[1].equals(objects2[1]) && !objects[1].equals("timestamp")) {
						JOptionPane.showMessageDialog(contentPane,
								"数据类型不匹配:" + "源数据类型[" + objects[1] + "]目标数据类型[" + objects2[1] + "]", "提示",
								JOptionPane.WARNING_MESSAGE);
						return;
					} else if (Integer.parseInt(objects[2]) > Integer.parseInt(objects2[2])
							&& !objects[1].equals("timestamp")) {// 为避免问题，避免数据长度变小
						JOptionPane.showMessageDialog(contentPane,
								"数据长度不配置:源数据长度[" + objects[2] + "]目标数据库长度[" + objects2[2] + "]", "提示",
								JOptionPane.WARNING_MESSAGE);
						return;
					}

					else {
						if (objects[1].equals("timestamp") && !objects2[1].equals("varchar")) {
							JOptionPane.showMessageDialog(contentPane, "timestamp类型只能映射varchar类型", "提示",
									JOptionPane.WARNING_MESSAGE);
							return;

						} else {

							if (map.containsKey(objects[0])) {
								// map.remove(objects[0]);
								JOptionPane.showMessageDialog(contentPane, objects[0] + "已经添加映射", "提示",
										JOptionPane.WARNING_MESSAGE);
								return;

							}

							else {

								map.put(objects[0], objects2[0]);
								list.clear();
								for (Map.Entry<String, String> entry : map.entrySet()) {

									String[] rdata = new String[2];
									rdata[0] = entry.getKey();
									rdata[1] = entry.getValue();
									list.add(rdata);
								}
							}
						}
					}

					tableData = new String[list.size()][];
					for (int k = 0; k < list.size(); k++) {
						tableData[k] = list.get(k);
					}

					tableModels.setDataVector(tableData, strings1);
					tableModels.fireTableDataChanged();
					setReturnString(JSON.toJSONString(map));
				}

				else {
					if (i < 0) {
						JOptionPane.showMessageDialog(contentPane, "没有选择源字段", "提示", JOptionPane.WARNING_MESSAGE);
						return;
					}
					if (j < 0) {
						JOptionPane.showMessageDialog(contentPane, "没有选择目标字段", "提示", JOptionPane.WARNING_MESSAGE);
						return;
					}
				}

			}
		});
		JButton button2 = new JButton("取消");
		button2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();

			}
		});
		JToolBar toolBar = new JToolBar();
		toolBar.add(button);
		toolBar.addSeparator();
		toolBar.add(button2);
		toolBar.setFloatable(true);

		jPanel.setPreferredSize(new Dimension(200, 100));
		jPanel.setViewportView(jTable);
		text1.add(toolBar);
		contentPane.add(text1, BorderLayout.NORTH);
		contentPane.add(jScrollPane1, BorderLayout.WEST);
		contentPane.add(jScrollPane, BorderLayout.EAST);
		contentPane.add(jPanel, BorderLayout.SOUTH);

		setContentPane(contentPane);
	}

}
